<template>
  <div class="data-set">
    <div class="value">{{ value }}</div>
    <div class="title">{{ title }}</div>
  </div>
</template>

<script>
export default {
  name: "DataSet",
  props: {
    title: {
      type: String,
      required: true
    },
    url: {
      type: String,
      required: true
    },
    interval: {
      type: Number,
      default: 0,
      required: false
    }
  },
  data () {
    return {
      value: '--'
    }
  },
  mounted () {
    this.flush();
    if (this.interval > 0) {
      this.$nextTick(function () {
        setInterval(this.flush, this.interval)
      })
    }
  },
  methods: {
    flush () {
      this.axios.get(this.url)
          .then((response) => {
            if (response.data.code === 200) {
              this.value = response.data.data
            } else {
              this.value = '--'
            }
          })
    }
  }
}
</script>

<style scoped>
.data-set {
  border: 1px solid #dddddd;
  padding: 10px;
  margin: 10px;
}
.value {
  color: #409EFF;
  margin: 10px 0;
  text-align: center;
  font-size: 50px;
}
.title {
  color: #999999;
  text-align: center;
  font-size: 14px;
  padding-bottom: 10px;
}
</style>